package com.tencent.examples.core.processor.mq;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pcbsys.nirvana.client.*;
import com.tencent.bk.utils.blueking.properties.MqProperties;
import com.tencent.examples.core.processor.MessageProcessor;
import com.tencent.examples.core.processor.common.BkobjidEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author :  CHENWEN
 * @Description : mq监听
 * @Creation Date:  2019/3/28
 */
@Service
public class MQConnect {
    private static final Logger logger = LoggerFactory.getLogger(MQConnect.class);
    public static final String CATTRIB_NAME = "LDBMdaStoreInfo";//获取门店信息
    public static final String CATTRIB_NAME_HOURS = "LDBMdaStoreHours";//获取门店时间信息

    @Autowired
    MqProperties mqProperties;
    @Autowired
    MessageProcessor messageProcessor;

    public void run() {
        try {
            //mq门店信息
            nSession mySession = nSessionFactory
                    .create(new nSessionAttributes(new String[]{mqProperties.getRealm()}), mqProperties.getUser(), mqProperties.getPassword());
            mySession.init();
            nChannelAttributes cattrib = new nChannelAttributes();
            cattrib.setName(CATTRIB_NAME);
            nChannel myChannel = mySession.findChannel(cattrib);
            StoreEventListener matr = new StoreEventListener(myChannel);

            //mq门店时间信息
            nChannelAttributes cattrib_hours = new nChannelAttributes();
            cattrib_hours.setName(CATTRIB_NAME_HOURS);
            nChannel myChannel_hours = mySession.findChannel(cattrib_hours);
            StoreHoursEventListener matr_hours = new StoreHoursEventListener(myChannel_hours);
            logger.info("连接成功");
        } catch (Exception e1) {
            logger.info("连接失败");
        }
    }

    /**
     * 监听回调--门店信息
     */
    class StoreEventListener implements nEventListener {
        StoreEventListener(nChannel myChannel) throws Exception {
            nNamedObject nobj = myChannel.createNamedObject(mqProperties.getNamedobject(), 0, true);
            myChannel.addSubscriber(this, nobj);
        }

        @Override
        public void go(nConsumeEvent nConsumeEvent) {
            String data = new String(nConsumeEvent.getEventData());
//            logger.info("MQ接受的数据,门店信息--->{}" + data);
            try {
                JSONArray jsonArray = JSONObject.parseArray(data);
                for (int i = 0; i < jsonArray.size(); i++) {
                    try {
                        JSONObject syncData = jsonArray.getJSONObject(i);
                        messageProcessor.syncStore(BkobjidEnum.jk, syncData, 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } catch (Exception e) {
                logger.error("mq消息返回格式错误");
            }

        }
    }


    /**
     * 监听回调--门店时间信息
     */
    class StoreHoursEventListener implements nEventListener {
        StoreHoursEventListener(nChannel myChannel) throws Exception {
            nNamedObject nobj = myChannel.createNamedObject(mqProperties.getNamedobject(), 0, true);
            myChannel.addSubscriber(this, nobj);
        }

        @Override
        public void go(nConsumeEvent nConsumeEvent) {
            String data = new String(nConsumeEvent.getEventData());
//            String data = "[{\"global_id\":\"27167\",\"store_name\":\"武汉大润发店\",\"day_of_week_mon1_sun7\":\"1\",\"business_open\":\"Y\",\"open_24_hours\":\"N\",\"business_open_time\":\"07:30:00\",\"business_close_time\":\"22:30:00\",\"begin_date\":\"2019-01-20\",\"end_date\":\"2019-02-03\"},{\"global_id\":\"27167\",\"store_name\":\"武汉大润发店\",\"day_of_week_mon1_sun7\":\"2\",\"business_open\":\"Y\",\"open_24_hours\":\"N\",\"business_open_time\":\"07:30:00\",\"business_close_time\":\"22:30:00\",\"begin_date\":\"2019-01-20\",\"end_date\":\"2019-02-03\"},{\"global_id\":\"27167\",\"store_name\":\"武汉大润发店\",\"day_of_week_mon1_sun7\":\"3\",\"business_open\":\"Y\",\"open_24_hours\":\"N\",\"business_open_time\":\"07:30:00\",\"business_close_time\":\"22:30:00\",\"begin_date\":\"2019-01-20\",\"end_date\":\"2019-02-03\"},{\"global_id\":\"27167\",\"store_name\":\"武汉大润发店\",\"day_of_week_mon1_sun7\":\"4\",\"business_open\":\"Y\",\"open_24_hours\":\"N\",\"business_open_time\":\"07:30:00\",\"business_close_time\":\"22:30:00\",\"begin_date\":\"2019-01-20\",\"end_date\":\"2019-02-03\"},{\"global_id\":\"27167\",\"store_name\":\"武汉大润发店\",\"day_of_week_mon1_sun7\":\"5\",\"business_open\":\"Y\",\"open_24_hours\":\"N\",\"business_open_time\":\"07:30:00\",\"business_close_time\":\"22:30:00\",\"begin_date\":\"2019-01-20\",\"end_date\":\"2019-02-03\"},{\"global_id\":\"27167\",\"store_name\":\"武汉大润发店\",\"day_of_week_mon1_sun7\":\"6\",\"business_open\":\"Y\",\"open_24_hours\":\"N\",\"business_open_time\":\"07:30:00\",\"business_close_time\":\"22:30:00\",\"begin_date\":\"2019-01-20\",\"end_date\":\"2019-02-03\"},{\"global_id\":\"27167\",\"store_name\":\"武汉大润发店\",\"day_of_week_mon1_sun7\":\"7\",\"business_open\":\"Y\",\"open_24_hours\":\"N\",\"business_open_time\":\"07:30:00\",\"business_close_time\":\"22:30:00\",\"begin_date\":\"2019-01-20\",\"end_date\":\"2019-02-03\"}]";
//            logger.info("MQ接受的数据,门店时间信息--->{}" + data);
            //查询出global_id对应的实列
            try {
                JSONArray jsonArray = JSONObject.parseArray(data);
                for (int i = 0; i < jsonArray.size(); i++) {
                    try {
                        JSONObject syncData = jsonArray.getJSONObject(i);
                        messageProcessor.syncStoreHours(BkobjidEnum.jk, syncData, 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } catch (Exception e) {
                logger.error("mq消息返回格式错误");
            }

        }
    }
}
